package com.common.sdk.net.connect.fresco;

import android.graphics.Bitmap;
import android.net.Uri;

import com.android.sohu.sdk.common.toolbox.LogUtils;
import com.facebook.cache.common.CacheKey;
import com.facebook.common.references.CloseableReference;
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory;
import com.facebook.imagepipeline.bitmaps.SimpleBitmapReleaser;
import com.facebook.imagepipeline.request.BasePostprocessor;

/**
 * Created by sangchengjiang on 16/8/10.
 */
public class CachedPostprocessor extends BasePostprocessor {

    private static final String TAG = "CachedPostprocessor";

    private int width;
    private int height;
    private String url;
    private CacheKey cacheKey;

    public CachedPostprocessor(int width, int height, String url) {
        this.width = width;
        this.height = height;
        this.url = url;
        cacheKey = new PostprocessorCacheKey(width, height, Uri.parse(url));
    }

    public CachedPostprocessor(int width, int height, Uri uri) {
        this.width = width;
        this.height = height;
        this.url = uri.toString();
        cacheKey = new PostprocessorCacheKey(width, height, uri);
    }

    @Override
    public CloseableReference<Bitmap> process(
            Bitmap sourceBitmap,
            PlatformBitmapFactory bitmapFactory) {


        CloseableReference<Bitmap> destBitmapRef = null;
        CloseableReference<Bitmap> scaleBitmapRef = null;

        int dstWidth = width;
        int dstHeight = height;

        try {

            if (sourceBitmap == null || dstWidth <= 0 || dstHeight <= 0) {
                return null;
            }

            int srcWidth = sourceBitmap.getWidth();
            int srcHeight = sourceBitmap.getHeight();

            // 对于没有变化的图片不需要裁切缩放
            if (srcWidth == dstWidth && srcHeight == dstHeight) {
                LogUtils.d(TAG, "request of url " + url + " 's width and height equals goal's width and height, no need to crop");
                return super.process(sourceBitmap, bitmapFactory);
            }

            // 先判断比例相等的
            if (dstWidth * srcHeight == dstHeight * srcWidth) {
                if (dstWidth < srcWidth) {
                    // 仅往小尺寸缩放
                    destBitmapRef = bitmapFactory.createScaledBitmap(sourceBitmap, dstWidth, dstHeight, false);
                    LogUtils.d(TAG, "request of url " + url + " was crop to " + dstWidth + "*" + dstHeight);
                    return destBitmapRef;
                } else {
                    LogUtils.d(TAG, "request of url " + url + " 's width and height is same scale with goal's width and height and goal's width and height is larger, no need to crop");
                    return super.process(sourceBitmap, bitmapFactory);
                }
            }

            int tmpWidth = 0;
            int tmpHeight = 0;

            if (dstWidth * srcHeight > dstHeight * srcWidth) {
                // 截取上下两部分
                if (dstWidth < srcWidth) {
                    // 先对原图进行等比缩放
                    tmpWidth = dstWidth;
                    tmpHeight = tmpWidth * srcHeight / srcWidth;
                }
            } else {
                // 截取左右两部分
                if (dstHeight < srcHeight) {
                    // 先对原图进行等比缩放
                    tmpHeight = dstHeight;
                    tmpWidth = srcWidth * tmpHeight / srcHeight;
                }
            }

            if (tmpWidth != 0 && tmpHeight != 0) {
                scaleBitmapRef = bitmapFactory.createScaledBitmap(sourceBitmap, tmpWidth, tmpHeight, false);
            }

            Bitmap scaleBitmap = null;
            if (scaleBitmapRef != null) {
                scaleBitmap = scaleBitmapRef.get();
            } else {
                scaleBitmap = sourceBitmap;
            }

            if (scaleBitmap == null) {
                return null;
            }

            int x = 0;
            int y = 0;

            int sw = scaleBitmap.getWidth();
            int sh = scaleBitmap.getHeight();
            int imgWidth = 0;
            int imgHeight = 0;
            if (sw * dstHeight < sh * dstWidth) {
                imgWidth = sw;
                imgHeight = sw * dstHeight / dstWidth;
                // 切除上下部分
                x = 0;
                y = (sh - imgHeight) / 2;
            } else {
                imgHeight = sh;
                imgWidth = sh * dstWidth / dstHeight;
                y = 0;
                x = (sw - imgWidth) / 2;
            }

            if (x == 0 && y == 0) {
                // 没有缩放的图片
                return super.process(sourceBitmap, bitmapFactory);
            }

            destBitmapRef = bitmapFactory.createBitmap(scaleBitmap, x, y, imgWidth, imgHeight);

            LogUtils.d(TAG, "request of url " + url + " was crop to " + imgWidth + "*" + imgHeight);

            return destBitmapRef;
        } catch (OutOfMemoryError e) {
            LogUtils.e(TAG, "getCenterInBitmap OutOfMemoryError :", e);
            return null;
        }

    }

    public CacheKey getPostprocessorCacheKey() {
        return cacheKey;
    }

}
